package net.gdou.doapp.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class DtsChannelImpl implements DtsChannel {
	
	private String filePath = null;
	private int dataSkipLineCount = 26;
	private int ctlSplitVal = 9;
	private int ctlLineEndVal = 13;
	private long cacheSize = 1024 * 1024; // 1Mb;
	
	private Map properties = null;
	private List data = null;
	
	public DtsChannelImpl(String filePath) {
		File f = new File(filePath);
		if (f.exists()) {
			this.filePath = filePath;
		}
	}

	public InputStream getRawStream() {

		FileInputStream fin = null;
		BufferedInputStream bin = null;
		try {
			fin = new FileInputStream(this.filePath);
			bin = new BufferedInputStream(fin);
			
		} catch (IOException ioe) {
			throw new RuntimeException(ioe);
		}  
		return bin;
	}
	
	protected boolean parseRawData() {
		
		FileReader fr = null;
		LineNumberReader lnr = null;
		File f = new File(this.filePath);
		try {
			if (f.exists()) { 
				fr = new FileReader(this.filePath); 
				lnr = new LineNumberReader(fr);
				String split = new String(new byte[]{(byte) this.ctlSplitVal});
				String currentLine = null;
				String[] propArray = null; 
				Map entity = null;
				while (null != (currentLine = lnr.readLine())) {
					
					if (lnr.getLineNumber() < this.dataSkipLineCount) {
						
						System.out.println("currentLine-> " + lnr.getLineNumber() + " <->"+ currentLine);
						
						propArray = currentLine.split(split);
						if (propArray.length == 2) {
							if (null == this.properties) {
								this.properties = new HashMap();
							}
							this.properties.put(propArray[0], propArray[1]);
						}
					} else if (lnr.getLineNumber() > this.dataSkipLineCount) { 
						StringTokenizer st = new StringTokenizer(currentLine); 
						entity = new HashMap(4, 1);
						entity.put("length", st.nextToken());
						entity.put("temperature", st.nextToken());
						entity.put("stokes", st.nextToken());
						entity.put("antiStokes", st.nextToken());
						if (null == this.data) {
							this.data = new ArrayList(20000);
						}
						this.data.add(entity); 
					}  
				}
				return true;
			} else {
				return false;
			}
		} catch (Exception ex) {
			return false;
		} finally { 
			if (null != lnr) {
				try {
					lnr.close();
				} catch (IOException e) { }
			}
			if (null != fr) {
				try {
					fr.close();
				} catch (IOException e) { }
			}
		} 
	}

	public List<Map> dataList() {
		
		if (null == this.data) { 
			if (!this.parseRawData()) {
				throw new RuntimeException("invalid data found!");
			}
		}
		return this.data ;
	}

	public Map propertiesMap() {
		
		if (null == this.properties) {
			if (!this.parseRawData()) {
				throw new RuntimeException("invalid data found!");
			}
		}
		return this.properties ;
	} 
	
	public static void main(String[] args) {
		
		String file = "E:/xiaogang/Sensornet DTS/data/full data set/Test/Test/2008/feb/Test 20080229 002 00001.ddf";
		DtsChannelImpl impl = new DtsChannelImpl(file);
		
		List<Map> rtv = impl.dataList();
		for (Map item : rtv) {
			System.out.println(item.toString());
		}
	} 
}
